/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package chameleon.view.command;

import chameleon.view.InputFactory;
import chameleon.view.input.Input;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author simon
 */
public class ReadModelCmd extends Command {

    private String toolName_;
    private String fileName_;

    public ReadModelCmd(Input input, String fileName, boolean help){
        super(input,false,help);
        toolName_ = "NuGat";
        fileName_ = fileName;
    }

    @Override
    public Input execute() {
        if (!help_){
            int tagTool;

            if ((tagTool=getToolTag(toolName_))!= -1){
                input_.setToolName(tagTool);
            }
            else{
                outputManager_.displayString("Unsported tool\n");
                return null;
            }

            try {
                File smv;
                if ((smv = getSmvFile(fileName_))!= null){
                    input_ = InputFactory.addSmvFile(smv, input_);
                }
            } catch (IOException ex) {
                Logger.getLogger(ReadModelCmd.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        else{
            outputManager_.displayString("read_model read a model on base af file\n"
                    + "the syntax is  : read_model -modelFile\n"
                    + "modelFile is the link to the file\n");
        }
        return input_;
    }


    private File getSmvFile(String input) throws IOException{
         File inputFile = new File (input); // We create a file with our input

        /** Here two possibilites :
         * -we have a file
         * -we have an input error
         */

        if (inputFile.isFile()){
            //Check if we have a smv file
            String extension = input.substring(input.indexOf("." ));

            if (extension.matches(".smv")){
                return inputFile;
            }
            else{
                outputManager_.displayString("InputMain::uncorrect extension, please try again\n");
            }

        }
        else{
            outputManager_.displayString("It's not a file! Please try again\n");
        }

        return null;

    }
}
